﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.Serialization;
using System.Data.SqlClient;

namespace DragDraw.Service
{
    /// <summary>
    /// DataManager的SQL实现
    /// </summary>
    public class SqlDataManager//负责连接数据库
    {
        //static protected SqlConnection main_connection;

        private static string connectionStr;
        ///<summary>
        ///SqlManager的初始化函数
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static  SqlDataManager()
        {
            try
            {
                string dbPath = ServerUtils.GetLocalPath(@"App_Data\MainDataBase.mdf");
                connectionStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + dbPath + @";Integrated Security=True;Connect Timeout=30;User Instance=True;Asynchronous Processing=true";
                //main_connection.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MainDataBaseConnectionString"].ConnectionString;
                //这个是从web.config读取东西的，如果你要测试的是应用程序的话可以直接把connectionstring改了，不读web.config了。
            }
            catch (Exception error)//can not read xml
            {
                ErrorManager.Instance.LogMessage(connectionStr);
                Console.WriteLine(error.Message);
                throw new DataErrorException("SqlManager初始化出错！很可能是WebConfigurationManager读出ConnectionStrings时发生错误！", error);
            }
        }
        ///<summary>
        ///连接数据库
        ///</summary>
        ///<returns>返回新的连接</returns>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        public static SqlConnection OpenConnection()
        {
            SqlConnection con;
            try
            {
                con = new SqlConnection();
                con.ConnectionString = connectionStr;
                con.Open();
                Console.WriteLine("Connection is open.");
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                //con.Close();
                throw new DataErrorException("连接数据库的时候出错！连接字：\n" + connectionStr,error);

            }
            return con;
        }
        ///<summary>
        ///断开与数据库的连接
        ///</summary>
        ///<param name="connection">需要关闭的连接</param>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        public static void CloseConnection(SqlConnection connection)
        {
            try
            {
                connection.Close();
            }
            catch(Exception error)
            {
                throw new DataErrorException("关闭连接时出错", error);
            }
        }
    }
}

